import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class Message{
    public  String from;
    public  String to;
    public  String message;
}
@WebServlet("/message")
public class MessageWall extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理提交请求
        Message message = objectMapper.readValue(req.getInputStream(), Message.class);
        save(message);
        //设置响应类型
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write("{ \"ok\": true }");
    }

    private void save(Message message) {
        //把消息保存到数据库中
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //和数据库建立连接
            connection = DBUtil.getConnection();
            //构造SQL语句
            String sql = "insert into message values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, message.from);
            preparedStatement.setString(2, message.to);
            preparedStatement.setString(3, message.message);
            //执行SQL语句
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            DBUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //把消息列表中的全部消息返回给客户端
        //存储消息列表
        List<Message> messages = load();
        //这里运用ObjectMapper将java对象转化成json字符串
        String jsonString = objectMapper.writeValueAsString(messages);
        System.out.println(jsonString);
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(jsonString);
    }

    private List<Message> load() {
        //从数据库中获取到所有资源
        List<Message> messages = new ArrayList();
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            //和数据库建立连接
            connection = DBUtil.getConnection();
            //构造SQL语句
            String sql = "select * from message";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();//存放到resultset中
            //遍历resultset
            while (resultSet.next()) {
                Message message = new Message();
                message.from = resultSet.getString("from");
                message.to = resultSet.getString("to");
                message.message = resultSet.getString("message");
                //将message添加到messages中
                messages.add(message);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return messages;
    }
}



